require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'datetimepicker': '/js/lib/bootstrap-datetimepicker.min',
        'select2': '/js/lib/select2.min'
    },
    shim: {
        'bootstrap': ['jquery']
    }
});

require(['jquery', 'doT', 'datetimepicker', 'common', 'bootstrap', 'select2'], function($, doT, datetimepicker, common) {
    $(function() {
        var pageSize = 10;
        var tradeList;
        var page = 1;
        var pageNumber;
        var role = sessionStorage.getItem('role');
        if (role == 1) {
            $('.managerOnly').removeClass('managerOnly');
        }

        $('.datepicker').datetimepicker({
            format: 'yyyy-mm-dd',
            autoclose: true,
            maxView: 'decade',
            minView: 'month'
        });

        $('#tradeForm').on('submit', function(event) {
            page = 1;
            event.preventDefault();
            refreshTradeList(1);
            var sid;
            if (sessionStorage.getItem('school_id')) {
                sid = sessionStorage.getItem('school_id');
            } else {
                sid = $('#schoolSelect').val();
            }
            $.getJSON(common.urlBase + '/shop/school-shop/' + sid + '.json', function(result) {
                if (result.ok) {
                    var shopTmpl = doT.template($('#shopTmpl').text());
                    $('#shopSelect').html(shopTmpl(result.data));
                } else {
                    $('#noticeMsg').text('该学校还未添加饭堂，请先添加饭堂!');
                    $('#noticeModal').modal('show');
                }
            });
        });

        function getSchoolList() {
            $.getJSON(common.urlBase + '/school/allSchool.json', function(result) {
                var schoolTmpl = doT.template($('#schoolTmpl').text());
                $('#schoolSelect').html(schoolTmpl(result.data));
                $('#schoolSelect').select2();
            });
        }
        getSchoolList();

        function refreshTradeList(page) {
            var data = {};
            if (sessionStorage.getItem('school_id')) {
                data.school_id = sessionStorage.getItem('school_id');
            } else {
                data.school_id = $('#schoolSelect').val();
            }
            data.start_time = new Date($('#tradeDate').val()).getTime();
            data.end_time = common.addDay(data.start_time, 1);
            data.page = page;
            data.page_sise = pageSize;
            $.post(common.urlBase + '/transaction/youzancombined/get', data, function(result) {
                if (result.ok && result.data.obj.length > 0) {
                    console.log(result.data);
                    tradeList = result.data.obj;
                    refreshPaginator(result.data.total);
                    var tradeTmp = doT.template($('#tradeTmpl').text());
                    $('#tradeTable').html(tradeTmp(tradeList));
                    $('#tradeAlert').parent('.alert').hide();
                } else if (result.ok && result.data.obj.length === 0) {
                    $('#tradeAlert').text('这一天无交易记录！');
                    $('#tradeAlert').parent('.alert').show();
                    $('#tradeTable').html('');
                    $('.paginatorRow').hide();
                } else {
                    $('#tradeAlert').text('查询出错，请重试');
                    $('#tradeAlert').parent('.alert').show();
                    $('#tradeTable').html('');
                    $('.paginatorRow').hide();
                }
            }, 'json');
        }

        function pageChange(event, oldPage, newPage) {
            refreshTradeList(newPage);
            page = newPage;
        }

        function refreshPaginator(total) {
            $('#total').text(total);
            pageNumber = Math.ceil(total / pageSize);
            common.initPaginator($('#paginator'), page, 8, pageNumber, pageChange);
            $('.paginatorRow').show();
        }

        $('.tradeImport').on('click', function(event) {
            //初始化参数
            var data = {};
            data.tids = [];
            tradeList.forEach(function(trade) {
                data.tids.push(trade.trade_id);
            });
            data.tids = JSON.stringify(data.tids);
            if (sessionStorage.getItem('school_id')) {
                data.school_id = sessionStorage.getItem('school_id');
            } else {
                data.school_id = $('#schoolSelect').val();
            }
            data.shop_id = $('#shopSelect').val();
            $.post(common.urlBase + '/transaction/youzantrades/import', data, function(result) {
                if (result.ok) {
                    $('#noticeMsg').text('该页交易已成功导入!');
                    $('#noticeModal').modal('show');
                    if (page < pageNumber) {
                        $('#paginator').bootstrapPaginator('show', page + 1);
                    }
                }
            }, 'json');
            $('#noticeMsg').text('正在导入，请稍候...');
            $('#noticeModal').modal('show');
        });

    });
});
